//
// Created by Qiang on 2021/2/28.
//

#include  "includes/log.h"
#include <jni.h>
#include <stdio.h>


#define CLASS_NAME   "com/android/jnidemo01/MainActivity"

static jstring myhelloWorld(JNIEnv *env, jclass cls) {
    return env->NewStringUTF("HelloWorld");
}

static JNINativeMethod jniHelloWorl[] = {
        {
                "helloWorld",
                "()Ljava/lang/String;",
                (void *) myhelloWorld
        }
};


static bool registerNativeMethods(JNIEnv *env, const char *className, JNINativeMethod *jniMethods,
                                  int methods) {
    jclass clazz = env->FindClass(className);
    if (clazz == NULL) {
        return false;
    }

    return env->RegisterNatives(clazz, jniMethods, methods) >= 0;
}



JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {


    int jniMethodSize = sizeof(JNINativeMethod);
    JNIEnv *env = NULL;
    if (vm->GetEnv((void **) &env, JNI_VERSION_1_6) != JNI_OK) {
        return -1;
    }
    if (!registerNativeMethods(env, CLASS_NAME, jniHelloWorl, sizeof(jniHelloWorl) /
                                                              jniMethodSize)) {
        return -1;
    }
    LOGD("JNI Loaded");

    return JNI_VERSION_1_6;
}